<?php
App::uses('AppController', 'Controller');

class CategoryManageController extends AppController {
	var $helpers = array('Paginator','Html');
	public $user = "";
	var $components = array('Link','Strings');
	public function beforeFilter() {
		parent::beforeFilter();
		$this->user = parent::checkAdminOrManagerLogin();
	}
	/**
	* hiển thị 
	*/
	public function admin_index() {
		$this -> loadModel('Category');
		$this -> loadModel('RootCategory');
		$this->set("title_for_layout","Quản lý Danh mục");
		
		$rootCat = $this->RootCategory->findNameAndId();
		$rootCatId = $rootCat[0]['root_categories']['id'];
		
		if ($this -> request -> is('post')) {
			$rootCatId = $this -> request ->data['id'];
		}
		$this->set('rootCatId',$rootCatId);
		$this->set("rootCat",$rootCat);
		$data = $this->Category->findByRootCategoryId($rootCatId);
		 $this->set("data",$data);
		
	}
	
	public function admin_add(){
		$this -> loadModel('Category');
		$this -> loadModel('RootCategory');
		
		$rootCat = $this->RootCategory->findNameAndId();
		$this->set("rootCat",$rootCat);
		if ($this -> request -> is('post')) {
			$new = $this->request->data;
			$new['category_name'] = $this->Strings->replaceHTMLTag($new['category_name']);
			$new["short_link"] = $this->Link->getShortForCatagoryLink($new['category_name'], ".html");
			$new['deleted'] = 0;
			$new['created_by'] = $this->user['id'];
			$new['created_at'] = date("Y-m-d H:i:s");
			$new['updated_by'] = $this->user['id'];
			$new['updated_at'] = date("Y-m-d H:i:s");
			$this->Category->save($new);
			$this->Session->setFlash('Thêm danh mục thành công');
			$this->redirect(array('action' => 'index'));
		}
		
		$this->set("title_for_layout","Thêm Danh mục");
	}
	
	public function admin_edit($id){
		$this -> loadModel('Category');
		$this -> loadModel('RootCategory');
		
		$rootCat = $this->RootCategory->findNameAndId();
		$this->set("rootCat",$rootCat);
		
		$conditions = array("Category.category_id" => $id);
		
		$data = $this->Category->find('first', array('conditions' => $conditions));
		if(empty($data)){
			$this->redirect(array('action' => 'index'));
		}
		$this->set("data",$data);
		if ($this -> request -> is('post')) {
			$new = $this->request->data;
			$new['category_name'] = $this->Strings->replaceHTMLTag($new['category_name']);
			$new["short_link"] = $this->Link->getShortForCatagoryLink($new['category_name'], ".html");
			$new['updated_by'] = $this->user['id'];
			$new['updated_at'] = date("Y-m-d H:i:s");
			$this->Category->save($new);
			$this->Session->setFlash('Chỉnh sửa danh mục thành công');
			$this->redirect(array('action' => 'index'));
		}
		
		$this->set("title_for_layout","Sửa Danh mục");
	}
	
	public function admin_delete($id){
		$this -> loadModel('Category');
		$conditions = array("Category.category_id" => $id);
		$data = $this->Category->find('first', array('conditions' => $conditions));
		if(!empty($data)){
			$update['deleted'] = 1;
			$update['category_id'] = $id;
			$update['updated_by'] = $this->user['id'];
			$update['updated_at'] = date("Y-m-d H:i:s");
			$this->Category->save($update);
		}
		$this->redirect(array('action' => 'index'));
	}
	/*-------------------------root category--------------------------------*/
	public function admin_rindex() {
		$this -> loadModel('RootCategory');
		$this->set("title_for_layout","Quản lý Danh mục");
		$rootCat = $this->RootCategory->findNameAndId();
		$this->set("data",$rootCat);
	}
	public function admin_radd(){
		$this -> loadModel('RootCategory');
		if ($this -> request -> is('post')) {
			$new = $this->request->data;
			$new['category_name'] = $this->Strings->replaceHTMLTag($new['category_name']);
			$new["short_link"] = $this->Link->getShortForCatagoryLink($new['category_name']);
			$new['deleted'] = 0;
			$new['created_by'] = $this->user['id'];
			$new['created_at'] = date("Y-m-d H:i:s");
			$new['updated_by'] = $this->user['id'];
			$new['updated_at'] = date("Y-m-d H:i:s");
			$this->RootCategory->save($new);
			$this->Session->setFlash('Thêm danh mục thành công');
			$this->redirect(array('action' => 'rindex'));
		}
		
		$this->set("title_for_layout","Thêm Danh mục");
	}
	public function admin_redit($id){
		$this -> loadModel('RootCategory');
		
		$conditions = array("RootCategory.id" => $id);
		
		$data = $this->RootCategory->find('first', array('conditions' => $conditions));
		if(empty($data)){
			$this->redirect(array('action' => 'index'));
		}
		$this->set("data",$data);
		if ($this -> request -> is('post')) {
			$new = $this->request->data;
			$new['category_name'] = $this->Strings->replaceHTMLTag($new['category_name']);
			$new["short_link"] = $this->Link->getShortForCatagoryLink($new['category_name']);
			$new['updated_by'] = $this->user['id'];
			$new['updated_at'] = date("Y-m-d H:i:s");
			$this->RootCategory->save($new);
			$this->Session->setFlash('Chỉnh sửa danh mục thành công');
			$this->redirect(array('action' => 'rindex'));
		}
		
		$this->set("title_for_layout","Sửa Danh mục");
	}
	public function admin_rdelete($id){
		$this -> loadModel('RootCategory');
		$conditions = array("RootCategory.id" => $id);
		$data = $this->RootCategory->find('first', array('conditions' => $conditions));
		if(!empty($data)){
			$new['deleted'] = 1;
			$new['id'] = $id;
			$new['updated_by'] = $this->user['id'];
			$new['updated_at'] = date("Y-m-d H:i:s");
			$this->RootCategory->save($new);
			$this->RootCategory->deleteCategoryByRootId($id, $this->user['id']);
			$this->Session->setFlash('Xóa danh mục thành công');
		}
		
		$this->redirect(array('action' => 'rindex'));
	}

}
?>